package com.thx.dictionary.dao;


import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.thx.common.orm.PaginationDao;
import com.thx.dictionary.model.DictionaryValue;

@Repository
public class DictionaryValueDao extends PaginationDao<DictionaryValue> {
	
	public List<DictionaryValue> findByDicnCode(String dicnCode) {
		String hql = "from DictionaryValue where dicn.dicnCode=? order by dicvOrder";
		return createQuery(hql, dicnCode).list();
	}
	public List<DictionaryValue> findByDicnId(String dicnId) {
		String hql = "from DictionaryValue where dicn.dicnId=? order by dicvOrder";
		return createQuery(hql, dicnId).list();
	}
	public DictionaryValue findByValueAndDicCode(String value , String code) {
		String hql = "from DictionaryValue where dicValue=? and dicn.dicnCode=? order by dicvOrder";
		List<DictionaryValue> list = createQuery(hql, value,code).list();
		if(list!=null&&list.size()>0){
			return list.get(0);
		}
		return null ;
	}
	public List<DictionaryValue> findByIDTAndDicCode(String id,String text, String code) {
		if(StringUtils.isBlank(code)){
			return null ;
		}
		List<Object> objs =new ArrayList<Object>();
		String hql = "from DictionaryValue where dicn.dicnCode=? ";
		objs.add(StringUtils.strip(code));
		if(StringUtils.isNotBlank(id)){
			hql += " and dicvId=?";
			objs.add(StringUtils.strip(id));
		}
		if(StringUtils.isNotBlank(text)){
			hql += " and dicvText like ?";
			objs.add("%"+StringUtils.strip(text)+"%");
		}
		hql +=" order by dicvOrder";
		
		List<DictionaryValue> list = findPageList(hql, objs.toArray());
		return list;	
	}
}
